﻿

using SLBehaviourSystem;
using SLUISystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

public class BehaviourEditorMenu : EditorMenuBase
{
    
    public BehaviourEditorMenu(EditorWindow window) : base(window)
    {
        Init();
    }

    public void Init()
    {
        AddItem(new GUIContent("AddChild"), false, new GenericMenu.MenuFunction2(AddNodeCallback), "Data");
        AddItem(new GUIContent("RemoveNode"), false, new GenericMenu.MenuFunction2(RemoveNodeCallback), "Data");

    }

    private void AddNodeCallback(object obj)
    {
        BehaviourEditorNode node = ((BehaviourWindow)window).CurSelectNode;
        BehaviourEditorNode newNode = new BehaviourEditorNode();
        node.Childs.Add(newNode);
        newNode.Window = (BehaviourWindow)window;
        newNode.Data = new BehaviourTreeNodeJsonData();
        newNode.Childs = new List<BehaviourEditorNode>();
        newNode.Data.Id = BehaviourEditorNode.IdIndex++;
        node.Data.ChildIds.Add(newNode.Data.Id);
        newNode.Data.Datas = new List<SLAssetSystem.Key2ValueJsonData>();
        newNode.Data.ChildIds = new List<int>();
        newNode.rectInGraph = new Rect(node.rectInGraph);
        newNode.Parent = node;
        newNode.CheckLayout();
        node.CheckLayout();

        //newNode.SetPosition
        newNode.Window.ReCode();
        newNode.Window.ReLayout();
    }

    private void RemoveNodeCallback(object obj)
    {
        BehaviourEditorNode node = ((BehaviourWindow)window).CurSelectNode;
        
        node.Parent.Childs.Remove(node);
        node.Parent.Data.ChildIds.Remove(node.Data.Id);
        node.Parent.CheckLayout();
        node.Window.ReCode();
        node.Window.ReLayout();
    }
}